// 发送请求
// let username = "wzcwzcwzc"
// let password = "wzcwzcwzc"
// let shuj = axios({
//     method: 'post',
//     url: 'http://localhost/api/users/login',
//     data: {
//         username,
//         password,
//     }
// })
// console.log(shuj);

let token = localStorage.token;

let img = document.querySelector('.one')
let souc = document.querySelector('.souc')
let sc = document.querySelector('.sc')
let m = false
let gwcone = document.querySelector('.gwc')
let gwctwo = document.querySelector('.boxt')
let kfEl = document.querySelector('.kf')
let back = document.querySelector('.back')
let tanc = document.querySelector('.tanc')
let read = document.querySelector('.read')

let cartName = localStorage.cartName
let goodsTitle = document.querySelector('h3 span')
let goodsPrice = document.querySelector('.first span:nth-of-type(2)')
let n = false
try {
    void(async function isFav() {
        let favorites = await axios({
            method: 'post',
            url: 'http://localhost:9527/api/data/getFav',
            data: {
                "cartName": cartName
            }, 
            headers: {
                'Authorization': 'Bearer ' + token
              }
        })
        if (favorites.data.code == 0) {
            souc.src = '../../img/icon/like_fill.png'
            m = true
        } else if (favorites.data.code == 1) {
            souc.src = "../../img/icon/souc.png"
            m = false
        }
    })()
} catch (error) {
    if (error.response.status == 401) {
        // 鉴权失败
        alert('用户未登录，请登录后进行访问')
    }
}

void(async function getGoods() {
    let thisGoods = await axios({
        method: 'post',
        url: 'http://localhost:9527/api/data/getGoods',
        data: {
            "cartName": cartName
        }
    })
    let thisGoodsData = thisGoods.data.data.goods[0]

    let username = localStorage.username
    let cartImg = thisGoodsData.cartImg
    let cartDes = thisGoodsData.cartDes
    let cartSpc = thisGoodsData.cartSpc
    let cartPrice = thisGoodsData.cartPrice
    let cartType = thisGoodsData.cartType
    let _id = thisGoodsData._id


    img.innerHTML = `<img src="${cartImg}" alt="">`
    goodsTitle.textContent = cartDes
    goodsPrice.textContent = cartPrice
    let num = 1;
    thisGoodsData.cartSpc.split(',').forEach((e) => {
        let goodsSpc = document.createElement('span')
        let sizeEl = tanc.querySelector('.size')
        if (num === 1) {
            goodsSpc.innerHTML = `
                <span class="size_active">${e}</span>
            `
            num++
        } else {
            goodsSpc.innerHTML = `
                <span>${e}</span>
            `
        }
        sizeEl.append(goodsSpc)
    });

    let inputEl = tanc.querySelector('input')

    tanc.onclick = async function (e) {
        let spanList = tanc.querySelectorAll(".size span")
        let targetEl = e.target;
        if (!targetEl.classList.contains('title') && !targetEl.classList.contains('size') && targetEl.closest('.size')) {
            spanList.forEach(e => e.classList.remove('size_active'))
            targetEl.classList.add('size_active');
        }

        if (targetEl.classList.contains('less') && inputEl.value != 1) {
            inputEl.value--
        }

        if (targetEl.classList.contains('more')) {
            inputEl.value++
        }
        if (targetEl.classList.contains('read')) {

            let chooseType
            spanList.forEach((e) => {
                if (e.classList.contains('size_active')) {
                    chooseType = e.textContent
                }
            })
            try {
                console.log(username);
                let result = await axios({
                    method: 'post',
                    url: 'http://127.0.0.1:9527/api/carts/addCart',
                    data: {
                        "username": username,
                        "cartName": cartName,
                        "cartDes": cartDes,
                        "cartImg": cartImg,
                        "cartType": cartType,
                        "cartPrice": cartPrice,
                        "cartSpc": cartSpc,
                        "cartNum": inputEl.value * 1
                    }, 
                    headers: {
                        'Authorization': 'Bearer ' + token
                      }
                })
                tanc.style.display = "none"
            } catch (error) {
                if (error.response.status == 401) {
                    // 鉴权失败
                    alert('用户未登录，请登录后进行访问')
                    let timer = setTimeout(() => {
                        location.href = "../../html/LXW/login.html"
                    })
                }
            }
        }
    }





    let arr = []

    {
        /*  */
    }

    document.onclick = async function (el) {
        el = el.target
        if (el === sc) {
            alert('保存成功')
        }
        if (el === souc) {
            if (!m) {
                try {
                    let result = await axios({
                        method: 'post',
                        url: 'http://localhost:9527/api/data/addFav',
                        data: {
                            "username": username,
                            "cartName": cartName,
                            "cartDes": cartDes,
                            "cartImg": cartImg,
                            "cartType": cartType,
                            "cartPrice": cartPrice,
                            "cartSpc": cartSpc
                        },
                        headers: {
                            'Authorization': 'Bearer ' + token
                          }
                    })
                    el.src = "../../img/icon/like_fill.png"
                    m = true

                } catch (error) {
                    console.log(error);
                    if (error.response.status == 401) {
                        // 鉴权失败
                        alert('用户未登录，请登录后进行访问')
                        let timer = setTimeout(() => {
                            location.href = "../../html/LXW/login.html"
                        })
                    }
                }
            } else {
                el.src = "../../img/icon/souc.png"
                m = false
                let result = await axios({
                    method: 'post',
                    url: 'http://localhost:9527/api/data/delFav',
                    data: {
                        "cartName": cartName
                    }
                })
            }
        }

        if (el === gwcone) {
            location.href = '../LJP/shopping_car.html'
        }
        if (el === kfEl) {
            alert('客服电话1234567890')
        }
        if (el === back) {
            history.back()
        }
        if (el.classList.contains('boxt')) {
            tanc.style.display = "block"
        }
        if (el.classList.contains('del')) {
            tanc.style.display = "none"
        }

        if (el.classList.contains('boxth')) {
            location.href = "../../html/WXL/dindan.html"
        }
    }
})()